package block

import (
	"crypto/sha256"
	"encoding/asn1"
	"math/big"

	"github.com/hyperledger/fabric-protos-go/common"
)

type asn1Header struct {
	Number       *big.Int
	PreviousHash []byte
	DataHash     []byte
}

func blockHeaderBytes(b *common.BlockHeader) ([]byte, error) {
	asn1Header := asn1Header{
		PreviousHash: b.PreviousHash,
		DataHash:     b.DataHash,
		Number:       new(big.Int).SetUint64(b.Number),
	}
	result, err := asn1.Marshal(asn1Header)
	if err != nil {
		return nil, err
	}
	return result, nil
}

func blockHeaderHash(b *common.BlockHeader) ([]byte, error) {
	byt, err := blockHeaderBytes(b)
	if err != nil {
		return nil, err
	}
	sum := sha256.Sum256(byt)
	return sum[:], nil
}
